package com.ujoin.dao.imp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.UserActivity;
import com.ujoin.dao.IUserActivityDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserActivity entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.ujoin.bean.UserActivity
 * @author MyEclipse Persistence Tools
 */

public class UserActivityDAO extends HibernateDaoSupport implements IUserActivityDAO {
	private static final Logger log = LoggerFactory
			.getLogger(UserActivityDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String ACTIVITY_ID = "activityId";
	public static final String HAS_GRADED = "hasGraded";
	public static final String VALID = "valid";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#save(com.ujoin.bean.UserActivity)
	 */
	@Override
	public void save(UserActivity transientInstance) {
		log.debug("saving UserActivity instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#delete(com.ujoin.bean.UserActivity)
	 */
	@Override
	public void delete(UserActivity persistentInstance) {
		log.debug("deleting UserActivity instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findById(java.lang.Integer)
	 */
	@Override
	public UserActivity findById(java.lang.Integer id) {
		log.debug("getting UserActivity instance with id: " + id);
		try {
			UserActivity instance = (UserActivity) getHibernateTemplate().get(
					"com.ujoin.bean.UserActivity", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByExample(com.ujoin.bean.UserActivity)
	 */
	@Override
	public List findByExample(UserActivity instance) {
		log.debug("finding UserActivity instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserActivity instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from UserActivity as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByUserId(java.lang.Object)
	 */
	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByActivityId(java.lang.Object)
	 */
	@Override
	public List findByActivityId(Object activityId) {
		return findByProperty(ACTIVITY_ID, activityId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByHasGraded(java.lang.Object)
	 */
	@Override
	public List findByHasGraded(Object hasGraded) {
		return findByProperty(HAS_GRADED, hasGraded);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findByValid(java.lang.Object)
	 */
	@Override
	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all UserActivity instances");
		try {
			String queryString = "from UserActivity";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#merge(com.ujoin.bean.UserActivity)
	 */
	@Override
	public UserActivity merge(UserActivity detachedInstance) {
		log.debug("merging UserActivity instance");
		try {
			UserActivity result = (UserActivity) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#attachDirty(com.ujoin.bean.UserActivity)
	 */
	@Override
	public void attachDirty(UserActivity instance) {
		log.debug("attaching dirty UserActivity instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserActivityDAO#attachClean(com.ujoin.bean.UserActivity)
	 */
	@Override
	public void attachClean(UserActivity instance) {
		log.debug("attaching clean UserActivity instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IUserActivityDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IUserActivityDAO) ctx.getBean("UserActivityDAO");
	}
}